package net.zigin.nhi.could.waste.service.impl;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.util.FileUtil;
import net.zigin.nhi.could.waste.util.OSSQiniuUtil;
import net.zigin.nhi.could.waste.vo.SysUpgradeVo;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.dto.SysUpgradeDto;
import net.zigin.nhi.could.waste.util.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysUpgradeMapper;
import net.zigin.nhi.could.waste.model.SysUpgrade;
import net.zigin.nhi.could.waste.service.SysUpgradeService;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-14 20:30:11                                               
##################################################################*/


/**
 * SysUpgrade服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUpgradeServiceImpl
 * @date 2021-9-14 20:30:11
 */
@Service
public class SysUpgradeServiceImpl extends AbstractServiceImpl<SysUpgrade, String> implements SysUpgradeService {
    @Autowired
    private SysUpgradeMapper sysUpgradeMapper;
    @Autowired
    private OSSClientUtil ossClientUtil;
    @Autowired
    private OSSQiniuUtil ossQiniuUtil;

    @Override
    public BaseMapper<SysUpgrade, String> getMapper() {
        return sysUpgradeMapper;
    }


    @Override
    public String saveOrUpdate(SysUpgradeDto sysUpgradeDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        if (StringUtil.isNullOrEmpty(sysUpgradeDto.getId())) {
            sysUpgradeDto.setId(StringUtil.get32UUID());
            sysUpgradeDto.setCreateUser(tokenInfo.getLoginName());
            sysUpgradeDto.setCreateTime(new Date());
            sysUpgradeDto.setModifyTime(new Date());
            SysUpgrade sysUpgrade = ReflectUtil.copyProperties(sysUpgradeDto, SysUpgrade.class);
            sysUpgradeMapper.insert(sysUpgrade);
            return sysUpgrade.getId();
        } else {
            sysUpgradeDto.setModifyUser(tokenInfo.getLoginName());
            sysUpgradeDto.setModifyTime(new Date());
            SysUpgrade sysUpgrade = ReflectUtil.copyProperties(sysUpgradeDto, SysUpgrade.class);
            sysUpgradeMapper.updateByPrimaryKeySelective(sysUpgrade);
            return sysUpgrade.getId();
        }
    }

    @Override
    public List<SysUpgradeVo> getList(SysUpgradeDto sysUpgradeDto) {
        Map<String, Object> paramMap = getSysUpgradeParam(sysUpgradeDto);
        List<SysUpgrade> sysUpgrades = sysUpgradeMapper.selectListByPage(paramMap);
        if (sysUpgrades.isEmpty()) {
            return new ArrayList<>();
        }
        return ReflectUtil.copyProperties(sysUpgrades, SysUpgradeVo.class);
    }

    @Override
    public Integer getCount(SysUpgradeDto sysUpgradeDto) {
        Map<String, Object> paramMap = getSysUpgradeParam(sysUpgradeDto);
        return sysUpgradeMapper.selectCount(paramMap);
    }

    private Map<String, Object> getSysUpgradeParam(SysUpgradeDto sysUpgradeDto) {
        return ParamsMapUtil.getInstance()
                .addParam("name", sysUpgradeDto.getName())
                .addParam("num", sysUpgradeDto.getNum())
                .addParam("pageIndex", sysUpgradeDto.getPageIndex())
                .addParam("pageSize", sysUpgradeDto.getPageSize());
    }

    @Override
    public Map<String, Object> uploadFile(MultipartFile file) throws Exception {
        String url = null;
        Map<String, Object> result = new HashMap<>();
        try {
            url = ossQiniuUtil.uploadFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*SysUpgrade sysUpgrade = sysUpgradeMapper.getLastest();
        sysUpgrade.setUrl(url);
        sysUpgradeMapper.updateByPrimaryKeySelective(sysUpgrade);*/
        File tempFile = FileUtil.multipartFileToFile(file);
        long version = readAPK(tempFile);
        tempFile.deleteOnExit();
        result.put("num", version);
        result.put("url", url);
        return result;
    }

    /**
     * 读取apk 返回版本号
     */
    private long readAPK(File file) {
        try {
            ApkFile apkFile = new ApkFile(file);
            ApkMeta apkMeta = apkFile.getApkMeta();
            return apkMeta.getVersionCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1L;
    }


}